//
//  AudioController.m
//  FirstMatchThree
//
//  Created by Petar on 6/11/15.
//  Copyright (c) 2015 Nestorov Engineering. All rights reserved.
//

#import "AudioController.h"
#import <AVFoundation/AVFoundation.h>
#import "Globals.h"

@implementation AudioController
{
    
    NSMutableDictionary *audio;
    
}

+ (instancetype)sharedInstance
{
    
    static AudioController *instance = nil;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    
    return instance;
    
}


- (instancetype)init
{
    
    self = [super init];
    
    if ( self ) {
        
        audio = [NSMutableDictionary dictionary];
        
        [self preloadAudioEffects:kSoundFiles];
        [self preloadBGMusic];
        
    }
    
    return self;
    
}


- (void)preloadAudioEffects:(NSArray*)effectFileNames
{
    
    for (NSString *effect in effectFileNames) {
        
        //get the file path URL
        NSString *soundPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: effect];
        NSURL* soundURL = [NSURL fileURLWithPath: soundPath];
        
        //load the file contents
        NSError* loadError = nil;
        AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error: &loadError];
        
        //prepare the play
        player.numberOfLoops = 0;
        [player prepareToPlay];
        
        //add to the array
        audio[effect] = player;
        
    }
    
}


- (void)playEffect:(NSString*)name
{
    
    if ( audio[name] == nil ) {
        return;
    }
    
    AVAudioPlayer* player = (AVAudioPlayer*)audio[name];
    [player play];
    
}


- (void)stopEffect:(NSString *)name
{
    
    if ( audio[name] == nil ) {
        return;
    }
    
    AVAudioPlayer* player = (AVAudioPlayer*)audio[name];
    [player stop];
    player.currentTime = 0;
    
}


- (void)playButtonClick
{
    
    [self playEffect:kButtonClickSound];

}


- (void)preloadBGMusic
{

    //get the file path URL
    NSString *soundPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:kLoopBGMusic];
    NSURL *soundURL = [NSURL fileURLWithPath: soundPath];
    
    //load the file contents
    NSError *error = nil;
    AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error: &error];
    
    if ( error ) {
        NSLog(@"error: %@", error.localizedDescription);
        return;
    }
    
    player.numberOfLoops = -1;
    
    //add to the array
    audio[kLoopBGMusic] = player;
    
}


- (void)playBGMusic
{
    
    AVAudioPlayer *player = (AVAudioPlayer*)audio[kLoopBGMusic];
    
    if ( player.isPlaying == NO ) {
        
        player.volume = 0;
        [player play];
        
        for (int i = 0; i < 6; i++) {
            [self performSelector:@selector(increaseVolumeOfPlayer:) withObject:player afterDelay:0.4 * i];
        }
        
    }
    
}


- (void)stopBGMusic
{
    
    AVAudioPlayer *player = (AVAudioPlayer*)audio[kLoopBGMusic];
    
    NSInteger times = 7;
    
    for (int i = 0; i < times; i++) {
        [self performSelector:@selector(decreaseVolumeOfPlayer:) withObject:player afterDelay:0.4 * i];
    }
    
}


- (void)decreaseVolumeOfPlayer:(AVAudioPlayer *)player
{
    
    CGFloat currentVolume = player.volume;
    
    if ( currentVolume >= 0.05 ) {
        
        currentVolume -= 0.05;
        [player setVolume:currentVolume];
        
//        NSLog(@"Decrease to: %f", currentVolume);
        
    } else {
        
        NSLog(@"Stop VolumeOfPlayer");
        
        player.currentTime = 0;
        [player stop];
        
    }
    
}


- (void)increaseVolumeOfPlayer:(AVAudioPlayer *)player
{
    
    CGFloat currentVolume = player.volume;
    
    if ( currentVolume < 0.3 ) {
        
        currentVolume += 0.05;
        [player setVolume:currentVolume];
        
//        NSLog(@"Increase to: %f", currentVolume);
        
    }
    
}


@end
